import { getAreaIdByTunnelId, getAreaList } from '@/api/road'
import { useActiveObject } from '@/store/activeObject'
import { useAuthStore } from '@/store/auth'
import { useViewportStore } from '@/store/viewport'
import { storeToRefs } from 'pinia'
import { Raycaster, Vector2 } from 'three'
import { watchEffect } from 'vue'
import RoadDetail from '@/views/DataView/road/roadDetail/roadDetail.vue'
import { useDialog } from '@/hooks/useDialog'

const { openDialog } = useDialog()
export default function () {
  const viewportStore = useViewportStore()
  const { publicGroup, canvas, camera, css2DRenderer } =
    storeToRefs(viewportStore)
  const authStore = useAuthStore()
  const { setGlobalParamsItem } = authStore
  const { modeParams } = storeToRefs(authStore)

  const activeObjectStore = useActiveObject()
  const mouse = new Vector2()
  const raycaster = new Raycaster()
  raycaster.layers.set(0)
  watchEffect(() => {
    if (canvas.value && css2DRenderer.value && canvas.value) {
      // 选中元素
      css2DRenderer.value.domElement.addEventListener('mouseup', async (e) => {
        if (modeParams.value.model === 'backtracking') return
        if (canvas.value) {
          const { clientWidth, clientHeight } = canvas.value
          if (e.button === 0) {
            mouse.x = (e.offsetX / clientWidth) * 2 - 1
            mouse.y = -(e.offsetY / clientHeight) * 2 + 1
            if (publicGroup.value && camera.value) {
              raycaster.setFromCamera(mouse, camera.value)
              const intersects = raycaster.intersectObjects(publicGroup.value.children)
              if (intersects[0] && intersects[0].object.visible) {
                const obj = intersects[0].object
                const { data } = await getAreaIdByTunnelId(obj.uuid)
                setGlobalParamsItem('areaId', data)
                if (obj.parent.type === 'Group' && obj.parent.children.length > 1 && obj.parent.name !== '巷道' && obj.parent.name !== '工作面') {
                  setGlobalParamsItem('areaName', obj.parent.name)
                  activeObjectStore.setActiveVO(obj.parent)
                  return
                }
                setGlobalParamsItem('areaName', obj.name)
                activeObjectStore.setActiveVO(obj)
              } else {
                activeObjectStore.setActiveVO(undefined)
                setGlobalParamsItem('areaId', '')
                setGlobalParamsItem('areaName', '')
              }
            }
          }
        }
      })

      // 双击选中元素
      css2DRenderer.value.domElement.addEventListener('dblclick', async (e) => {
        if (canvas.value) {
          const { clientWidth, clientHeight } = canvas.value
          if (e.button === 0) {
            mouse.x = (e.offsetX / clientWidth) * 2 - 1
            mouse.y = -(e.offsetY / clientHeight) * 2 + 1
            if (publicGroup.value && camera.value) {
              raycaster.setFromCamera(mouse, camera.value)
              const intersects = raycaster.intersectObjects(publicGroup.value.children)
              if (intersects[0] && intersects[0].object.visible) {
                const obj = intersects[0].object
                const { data: areaId } = await getAreaIdByTunnelId(obj.uuid)
                const { data } = await getAreaList({ areaId })
                openDialog(data[0].areaName, RoadDetail, { ...data[0], active: '1' }, {
                  width: 950,
                  height: 400
                })
              }
            }
          }
        }
      })
    }
  })
}
